﻿<?php
	session_start();
	include_once "config.php";
	require_once ("classe/BD.php");
	$bd = new BD();
	$bd->conexao();
?>



<?php
		
		$usuarioLogin = $_POST['email'];
		$usuarioSenha = $_POST['senha'];
		
		if(isset($_POST['acao']) && $_POST['acao'] == 'logar'):
			$email = strip_tags(filter_input(INPUT_POST,'email',FILTER_SANITIZE_STRING));
			$senha = strip_tags(filter_input(INPUT_POST,'senha',FILTER_SANITIZE_STRING));
			if($email == '' && $senha == ''):
			else:
				$pegar_user = $bd->conexao()->prepare("SELECT * FROM usuario WHERE email = ? AND senha = ?");
				$pegar_user->execute(array($email, $senha));
				if($pegar_user->rowCount() == 0):
					echo '<script>alert("Usuário não encontrado");location.href="index.php"</script>';	
				 else: 
					$fetch = $pegar_user->fetchObject();
					
					$atual  = date('Y-m-d H:i:s');
					$expira = date('Y-m-d H:i:s', strtotime('+1 min'));
					$update = $bd->conexao()->prepare("UPDATE usuario SET horario = ?, limite = ? WHERE email = ? ");
					$update->execute(array($atual, $expira, $email));
					
					$_SESSION['id_user'] = $fetch->id;
					echo '<script>alert("Entrando...");location.href="chat.php"</script>';
				endif;	
			endif;
		endif;
		
		if($usuarioLogin == $email && $usuarioSenha == $senha):
			
			$_SESSION['usuarioSession'] = $usuarioLogin; 
			$_SESSION['senhaSession']   = $usuarioSenha;
			
			$_SESSION['valida'] = $fetch->id;
			header('location:chat.php');
			
			
			else:
				$_SESSION['valida'] = 0; 
		endif;
?>
